package com.hoow.shakoow.shake.adManager

import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.LoadAdError
import com.google.android.gms.ads.rewarded.RewardedAd
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback
import com.hoow.shakoow.base.BaseActivity

class RewardedAdManager(val activity: BaseActivity) {

    // 状态回调
    private var mAdsStateListener: AdsStateListener? = null

    // 广告句柄
    private var mRewardedAd: RewardedAd? = null

    // 广告归属id ca-app-pub-7210412947896074/7284406723
    //private val mRewardAdUnitId = "ca-app-pub-3940256099942544/5224354917"

    // 准备好后是否需要立即展示
    private var isNeedShowAdsWhenLoadFinish = false

    /**
     * 预加载广告
     */
    fun initAds() {
        if (mRewardedAd == null) {
            val adRequest = AdRequest.Builder().build()
            val rewId = AdsIdUtil.getRewUnitId()

            RewardedAd.load(
                activity,
                rewId,
                adRequest,
                object : RewardedAdLoadCallback() {
                    override fun onAdFailedToLoad(adError: LoadAdError) {
                        println("===> rewards ${adError.message}")
                        mRewardedAd = null
                    }

                    override fun onAdLoaded(ad: RewardedAd) {
                        println("===> rewards Ad was loaded.")
                        mRewardedAd = ad

                        if (isNeedShowAdsWhenLoadFinish) {
                            isNeedShowAdsWhenLoadFinish = false
                            showAds()
                        }
                    }
                }
            )
        }
    }

    /**
     * 显示广告
     */
    fun showAds() {
        if (mRewardedAd != null) {
            mRewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() {
                override fun onAdClicked() {
                    // Called when a click is recorded for an ad.
                    println("===> rewards Ad was clicked.")
                }

                override fun onAdDismissedFullScreenContent() {
                    // Called when ad is dismissed.
                    println("===> rewards Ad dismissed fullscreen content.")
                    mRewardedAd = null

                    // 重新加载
                    initAds()
                }

                override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                    // Called when ad fails to show.
                    println("===> rewards Ad failed to show fullscreen content.")
                    mRewardedAd = null
                }

                override fun onAdImpression() {
                    // Called when an impression is recorded for an ad.
                    println("===> rewards Ad recorded an impression.")

                }

                override fun onAdShowedFullScreenContent() {
                    // Called when ad is shown.
                    println("===> rewards Ad showed fullscreen content.")
                }
            }

            mRewardedAd?.show(activity) { rewardItem ->
                // Handle the reward.
                val rewardType = rewardItem.type
                val rewardAmount = rewardItem.amount
                println("===> rewards Ad User earned the reward. amount:$rewardAmount type:$rewardType")

                // 记录看完广告
                mAdsStateListener?.onRewardAdCompleted(rewardType, rewardAmount.toString())
            }
        } else {
            println("===> The rewarded Ad wasn't ready yet.")
            isNeedShowAdsWhenLoadFinish = true
            initAds()
        }
    }

    /**
     * 设置状态监听
     */
    fun setAdsStateListener(listener: AdsStateListener?) {
        mAdsStateListener = listener
    }
}